/*
 * 仪器设置合作标准的时候 生成 项目追踪数据
 */
public without sharing class GenTaskTrackingHandler
{

  public GenTaskTrackingHandler(){

    
  }
  /*
    public static void GenTaskTracking(List<Equipment__c> list_new,Map<Id,Equipment__c> map_old)
    {
        ////更新时 如果有新的合作标准 保存起来
        Set<Id> trackIds = new Set<Id>();
        Map<Id,Equipment__c> eMap= new Map<Id,Equipment__c>();
		Set<Id> proIds = new Set<Id>();
        for(Equipment__c e:list_new){
        	if(map_old.containsKey(e.Id) && e.CooperationStandard__c != map_old.get(e.Id).CooperationStandard__c){
            	trackIds.add(e.CooperationStandard__c);
               eMap.put(e.CooperationStandard__c,e);
                proIds.add(e.Product__c);
            }
        }
        ///保存生化的仪器
        Map<Id,String> shBrandMap = new Map<Id,String>();
        ///查询仪器一二级分类
        Map<Id,Product__c> proMap = new Map<Id,Product__c>();
       for(Product__c p :[select Id, Category1__c ,Category2__c from Product__c where Id in :proIds ]){
       		proMap.put(p.Id,p);
           System.debug(p.Category1__c+p.Id);
           if(p.Category1__c.contains('生化')){
           		shBrandMap.put(p.Id,'');
               System.debug('==='+p);
           }
       }
        ///如果是生化的仪器 查询对应所有生化-试剂的 品牌
        String brands = '';
        Set<String> brSet = new Set<String>();
        if(shBrandMap.keySet().size()> 0){
        	for(Product__c p :[select Id,Brand__c from Product__c where Category1__c = '生化' and (Category2__c = '生化-试剂' or Category2__c = '生化-试剂') and Brand__c !=null and Brand__c != '']){
            	brSet.add(p.Brand__c);
            }
        }
        ///组合品牌
        for(String b : brSet){
        	brands += b+',';
        }
        if(brands.length()> 0)brands.subString(0,brands.length()-1);
        
        ///合同任务量
        List<TaskTracking__c> trackList = new List<TaskTracking__c>();
        for(ContractAssignments__c ca : [select Id ,ReagentAssignments__c,NoTaxReagentAssignments__c,Contract__c,
                                        Contract__r.Corpcode__c,Contract__r.ContractNo__c,Contract__r.TerminalCustomer__r.Name,
                                        Contract__r.Account__c,Contract__r.Account__r.RecordType.Name,Contract__r.CompanyControls__c,
                                         CooperationStandard__r.PartsWarrantyFee__c,Contract__r.Account__r.System__c,
                                         CooperationStandard__r.ReagentAssignments__c,CooperationStandard__r.NoTaxReagentAssignments__c,
                                         CooperationStandard__r.ServiceCharge__c,CooperationStandard__r.InstrumentMargin__c,
                              			     CooperationStandard__r.TotalFee__c,Year__c,Account__c,CooperationStandard__c,ContractProduct__c,
                                         Contract__r.TerminalCustomer__r.NCICCode__c,Contract__r.Account__r.TureProvince__r.Name,
                                         CooperationStandard__r.ContractProduct__r.Product__c, CooperationStandard__r.ContractProduct__c
                                         from ContractAssignments__c where CooperationStandard__c in:trackIds])
        {
            if(eMap.containsKey(ca.CooperationStandard__c)){
            
            	///创建任务追踪
					TaskTracking__c tt = new TaskTracking__c();
            		tt.Company__c = ca.Contract__r.CompanyControls__c;///公司
            		tt.Contract__c = ca.Contract__c;///合同
            		tt.ContractNumber__c = ca.Contract__r.ContractNo__c;///合同编号
                  tt.ContractProduct__c = ca.CooperationStandard__r.ContractProduct__c;///合同产品
                  tt.Assignments__c = ca.Id;///合同任务量
            		tt.TerminalCustomer__c = ca.Contract__r.TerminalCustomer__r.Name;///终端客户
                  tt.Standers__c = ca.CooperationStandard__c;///合作标准
            		tt.CooperationStandaNoTaxReagentAssignments__c = ca.CooperationStandard__r.NoTaxReagentAssignments__c;///不含税合作标准 试剂任务量
            		tt.CooperationStandardReagentAssignments__c = ca.CooperationStandard__r.ReagentAssignments__c;///含税合作标准 试剂任务量
          			tt.TerminalCustomerPK__c = ca.Contract__r.TerminalCustomer__r.NCICCode__c;//终端客户识别码
            		//StartingDate__c///开始日期
                	//EndingDate__c///结束日期
                    tt.InstrumentName__c =eMap.containsKey(ca.CooperationStandard__c)? eMap.get(ca.CooperationStandard__c).Id : null;///仪器
                    tt.Account__c = ca.Contract__r.Account__c;///开票客户
                    tt.System__c = ca.Contract__r.Account__r.System__c;/// 第三方体系
                    tt.RecordType__c = ca.Contract__r.Account__r.RecordType.Name ;///开票客户类型
                    tt.Province__c = ca.Contract__r.Account__r.TureProvince__r.Name; ///开票客户省份
                    //Tax__c =  ///是否含税 手动维护
                     tt.Year__c = ca.Year__c; ///年度
                    tt.PartsWarrantyFee__c  = ca.CooperationStandard__r.PartsWarrantyFee__c;//配件保修费
                    tt.NoTaxReagentAssignments__c = ca.NoTaxReagentAssignments__c; ///试剂任务量不含税
                    tt.ReagentAssignments__c = ca.ReagentAssignments__c;///试剂任务量含税
                    tt.ServiceCharge__c = ca.CooperationStandard__r.ServiceCharge__c; ///维修服务费
                    //ProjectAnnotation2__c ///项目部批注
                    //ProjectAnnotation4__c ///销售部批注
                    //ProjectAnnotation1__c ///业务部批注
                	System.debug(eMap.get(ca.CooperationStandard__c).EndDate__c);
                  	 tt.EndDate__c  = eMap.containsKey(ca.CooperationStandard__c)?(eMap.get(ca.CooperationStandard__c).EndDate__c== null ? '' :eMap.get(ca.CooperationStandard__c).EndDate__c.format()):'';//仪器安装日期
                    tt.InstrumentMargin__c =ca.CooperationStandard__r.InstrumentMargin__c;///仪器保证金
                    tt.states__c  =eMap.containsKey(ca.CooperationStandard__c)? eMap.get(ca.CooperationStandard__c).OpponentStatus__c:null;///仪器合作状态
                    tt.SerialNo__c  = eMap.containsKey(ca.CooperationStandard__c)?eMap.get(ca.CooperationStandard__c).IMEINumber__c:null;///机身号
                    tt.InstrumentType__c  =eMap.containsKey(ca.CooperationStandard__c)? eMap.get(ca.CooperationStandard__c).Model__c:null;///仪器型号
                   	tt.Classification1__c =proMap.containsKey(ca.CooperationStandard__r.ContractProduct__r.Product__c)? proMap.get(ca.CooperationStandard__r.ContractProduct__r.Product__c).Category1__c : null; ///仪器一级分类
                
                    
                    tt.Classification2__c = proMap.containsKey(ca.CooperationStandard__r.ContractProduct__r.Product__c)? proMap.get(ca.CooperationStandard__r.ContractProduct__r.Product__c).Category2__c:null;///仪器二级分类
                	if(tt.Classification2__c !=null)tt.Classification2__c = tt.Classification2__c.replaceAll('设备','试剂');
                    tt.TotalFee__c  = ca.CooperationStandard__r.TotalFee__c;///应付当期费用
                	if(tt.Classification1__c != null && tt.Classification1__c.contains('生化') ){
                    	tt.ReagentBrand__c = brands;///试剂品牌
                    }else{
                       tt.ReagentBrand__c = eMap.containsKey(ca.CooperationStandard__c)?eMap.get(ca.CooperationStandard__c).Brand__c : null;
                    }
                	trackList.add(tt);
                  
            
            }
                    
                    
        }
        if(trackList.size() > 0) insert trackList;
        
        
    }*/
}